package org.crazyit.cloud.use1;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import rx.Observable;
import rx.Observer;
/**
 * Hystrix  命令的使用，这里不调用什么服务，直接就是命令的使用
 */
public class CommandRunMain {

    public static void main(String[] args) throws InterruptedException {
        RunCommand c1 = new RunCommand("observe method");
        //这里面会实现一个订阅，马上执行
        c1.observe();

        RunCommand c2 = new RunCommand("toObservable method");
        //它不会马上执行，它会返回一个可观察对象Observable。
        // 怎么让Observable执行呢？  订阅这个对象。
        Observable ob = c2.toObservable();
        ob.subscribe(new Observer() {
            public void onCompleted() {
                System.out.println("command Completed");
            }

            public void onError(Throwable throwable) {

            }

            public void onNext(Object o) {
                //这是命令执行的返回结果
                System.out.println("####" + o);
            }
        });
        Thread.sleep(1000);
    }

    static class RunCommand extends HystrixCommand<String>{
        String msg;
        public RunCommand(String msg) {
            super(HystrixCommandGroupKey.Factory.asKey("TestGroup"));
            this.msg = msg ;
        }

        @Override
        protected String run() throws Exception {
            System.out.println(msg);
            return "success";
        }
        
    }

}
